<!--
 * @Descripttion: 
 * @version: 编辑房源
 * @Author: mmzhang
 * @Date: 2021-06-29 09:57:27
 * @LastEditTime: 2021-09-24 11:27:07
-->
<template>
  <div class="edit-room">
    <Form ref="editRoomForm" :model="editRoomForm" :rules="ruleValidate" :label-width="100">
      <Form-item label="已选房源：" prop="checkRoom">
        <p>{{ editRoomCode.length }}</p>
      </Form-item>
      <Form-item label="面积：" prop="area">
        <Input type="text" v-model="editRoomForm.area" number>
          <span slot="append">㎡</span>
        </Input>
      </Form-item>
      <Form-item label="赠送面积：" prop="giveArea">
        <Input type="text" v-model="editRoomForm.giveArea" number>
          <span slot="append">㎡</span>
        </Input>
      </Form-item>
      <Form-item label="底单价：" prop="basicPrice">
        <Input type="text" v-model="editRoomForm.basicPrice" number>
          <span slot="append">元</span>
        </Input>
      </Form-item>
      <Form-item label="底总价：" prop="basicTotalPrice">
        <Input type="text" v-model="editRoomForm.basicTotalPrice" number>
          <span slot="append">元</span>
        </Input>
      </Form-item>
      <Form-item label="表单价：" prop="facePrice">
        <Input type="text" v-model="editRoomForm.facePrice" number>
          <span slot="append">元</span>
        </Input>
      </Form-item>
      <Form-item label="表总价：" prop="faceTotalPrice">
        <Input type="text" v-model="editRoomForm.faceTotalPrice" number>
          <span slot="append">元</span>
        </Input>
      </Form-item>
      <Form-item class="btn-operate">
        <Button @click="handleClose">取消</Button>
        <Button type="primary" @click="submitFun" style="margin-left: 8px">确定</Button>
      </Form-item>
    </Form>
  </div>
</template>
<script>
  import roomMgr from '@/biz/modeling/room/room'

  let numberPass = (rule, value, callback) => {
    let reg = /(^[1-9]\d*\.\d+$|^0\.\d+$|^[1-9]\d*$|^0$)/ ///^([0-9]+[\d]*(.[0-9]{1,2})?)$/
    if (value && !reg.test(value)) {
      callback(new Error('请输入数字,最多保留两位小数'))
    } else {
      callback()
    }
  }

  export default {
    props: ['editRoom', 'coopMode'],
    data() {
      return {
        editRoomForm: {
          area: '',
          giveArea: '',
          basicPrice: null,
          basicTotalPrice: null,
          facePrice: '',
          faceTotalPrice: ''
        },
        editRoomCode: []
      }
    },
    computed: {
      ruleValidate() {
        let basicRequired = this.coopMode == 4 || this.coopMode == 5
        return {
          area: [{ validator: numberPass, trigger: 'blur', required: false }],
          giveArea: [{ validator: numberPass, trigger: 'blur', required: false }],
          basicPrice: [
            { validator: numberPass, trigger: 'blur', required: basicRequired },
            {
              message: '包销项目底单价不可为空',
              trigger: 'blur',
              required: basicRequired,
              type: 'number'
            }
          ],
          basicTotalPrice: [
            { validator: numberPass, trigger: 'blur', required: basicRequired },
            {
              message: '包销项目底总价不可为空',
              trigger: 'blur',
              required: basicRequired,
              type: 'number'
            }
          ],
          facePrice: [{ validator: numberPass, trigger: 'blur', required: false }],
          faceTotalPrice: [{ validator: numberPass, trigger: 'blur', required: false }]
        }
      }
    },
    created() {
      this.editRoomCode = this.editRoom.map((item) => {
        return item.roomCode
      })
      if (this.editRoom.length == 1) {
        let item = this.editRoom[0]
        item.roomType = null
        this.editRoomForm = {
          ...item,
          basicPrice: Number(item.basicPrice),
          basicTotalPrice: Number(item.basicTotalPrice)
        }
      }
    },
    methods: {
      handleClose() {
        this.$refs['editRoomForm'].resetFields()
        this.$emit('onClose')
      },
      submitFun() {
        this.$refs['editRoomForm'].validate((valid) => {
          if (valid) {
            let roomArr = []
            this.editRoom.forEach((ele) => {
              let obj = Object.assign({ roomId: ele.roomId }, this.editRoomForm)
              roomArr.push(obj)
            })
            let params = {
              roomEditVOS: roomArr
            }
            roomMgr.editRoomFun(params).then((res) => {
              this.$Message.info('修改成功。')
              this.$emit('onClose')
              this.$emit('onSearch')
            })
          } else {
            this.$Message.error('房源编辑请输入数字,最多保留两位小数!')
          }
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .edit-room {
    ::v-deep .ivu-form-item-content {
      display: flex;
    }

    .btn-operate ::v-deep .ivu-form-item-content {
      justify-content: flex-end;
    }
  }
</style>
